package controllers

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"lottery_code/comm"
	"lottery_code/models"
	"lottery_code/services"
)

type IndexController struct {
	Ctx            iris.Context
	ServiceUser    services.UserService
	ServiceBlackip services.BlackipService
	ServiceCode    services.CodeService
	ServiceGift    services.GiftService
	ServiceResult  services.ResultService
	ServiceUserDay services.UserDayService
}

// http://
func (c *IndexController) Get() string {
	c.Ctx.Header("Content-Type", "text/html")
	return "welcome to Go抽奖系统课,<a href='./public/index.html'>开始抽奖</a>"
}

func (c *IndexController) GetGifts() map[string]interface{} {
	rs := make(map[string]interface{})
	rs["code"] = 0
	rs["msg"] = ""
	datalist := c.ServiceGift.GetAll(true)
/*	list := make([]models.LtGift, 0)
	for _, data := range datalist {
		if data.SysStatus == 0 {
			list = append(list, data)
		}
	}*/
	rs["gifts"] = datalist
	return rs
}

func (c *IndexController) GetNewprize() map[string]interface{} {
	rs := make(map[string]interface{})
	rs["code"] = 0
	rs["msg"] = ""
	//TODO:

	return rs
}

func (c *IndexController)GetLogin(){
	uid :=comm.Random(10000)
	loginuser:=&models.ObjLoginuser{
		Uid:      uid,
		Username: fmt.Sprintf("admin-%d",uid),
		Now:      comm.NowUnix(),
		Ip:       comm.ClientIp(c.Ctx.Request()),
	}
	comm.SetLoginuser(c.Ctx.ResponseWriter(),loginuser)
	comm.Redirect(c.Ctx.ResponseWriter(),
		"/public/index.html?form=login")
}

func (c *IndexController)GetLogout(){
	comm.SetLoginuser(c.Ctx.ResponseWriter(),nil)
	comm.Redirect(c.Ctx.ResponseWriter(),
		"/public/index.html?from=logout" )
}


